<script setup>
import { ref, computed, onMounted } from 'vue';
import Charts1 from '../components/charts1.vue';

const props = defineProps({
  sensors: {
    type: Array,
    required: true,
  }
});
const sensors = computed(() => {
  updateTimes.value ++;
  refreshData();
});

const MidC = ref(null), RunT = ref(null), PowerCost = ref(null), CO2 = ref(null), TemLine = ref(null), PowerUse = ref(null);
const RunTC_dex = ref(0), TemLineC_dex = ref(0), PowerUseC_dex = ref(0);

const updateTimes = ref(0);
refreshData();
setInterval(() => {
  updateTimes.value ++;
  refreshData();
}, 10000);
function refreshData() {
  const [sensors0, sensors1, sensors2, sensors3, sensors4, sensors5] = props.sensors;
  MidC.value = sensors0, RunT.value = sensors1, PowerCost.value = sensors2, CO2.value = sensors3, TemLine.value = sensors4, PowerUse.value = sensors5;
  
}

function changeValue(value) {
  if (value) {
    if (value > 0 && value <= 10) {
      return Math.floor((value / 10) * 50);
    } else if (value > 10 && value <= 100) {
      return Math.floor((value / 100) * 50);
    } else if (value > 100 && value <= 500) {
      return Math.floor((value / 500) * 50);
    } else if (value > 500 && value <= 1000) {
      return Math.floor((value / 1000) * 50);
    } else if (value > 1000 && value <= 5000) {
      return Math.floor((value / 5000) * 50);
    } else if (value > 5000 && value <= 10000) {
      return Math.floor((value / 10000) * 50);
    }
  }
};

let domPowerUseList = [];
onMounted(() => {
  domPowerUseList = document.querySelectorAll('.powerUse.tabLine>.tabList>.tabItem');
});
function PowerUseClick(index) {
  PowerUseC_dex.value = index;
  domPowerUseList[PowerUseC_dex.value].scrollIntoView({inline: 'center'});
}
function PowerUseGoFront() {
  PowerUseC_dex.value = PowerUseC_dex.value >= 1 ? PowerUseC_dex.value - 1 : 0;
  domPowerUseList[PowerUseC_dex.value].scrollIntoView({inline: 'center'});
}
function PowerUseGoAfter() {
  PowerUseC_dex.value = PowerUseC_dex.value <= PowerUse.value.c.length - 2 ? PowerUseC_dex.value + 1 : PowerUse.value.c.length - 1;
  domPowerUseList[PowerUseC_dex.value].scrollIntoView({inline: 'center'});
}
</script>

<template>
  <div class="left">
    <template v-if="RunT?.c?.length">
      <div class="title flex--center">
        <div class="t_name">{{ RunT.n }}</div>
        <el-select class="select" size="small" v-model="RunTC_dex" v-if="RunT.c?.length > 1">
          <el-option v-for="(item, index) in RunT.c" :key="index" :value="index" :label="item.n" />
        </el-select>
      </div>
      <div class="run-list" v-if="RunT.c && RunT.c[RunTC_dex]?.c?.length">
        <div class="run-item" v-for="(item, index) in RunT.c[RunTC_dex].c" :key="index">
          <template v-if="item.value < 10000">
            <div class="run-num">{{ item.value }}</div>
            <div class="run-desc">{{ item.n }}</div>
            <div class="run-unit"> {{ item.unit }}</div>
          </template>
          <template v-else>
            <div class="run-num">{{ (Number(item.value)/10000).toFixed(0) }}</div>
            <div class="run-desc">{{ item.n }}</div>
            <div class="run-unit">万{{ item.unit }}</div>
          </template>
        </div>
      </div>
    </template>
    
    <template v-if="PowerCost?.c?.length">
      <div class="title">
        <div class="t_name">{{ PowerCost.n }}</div>
      </div>
      <div class="run-list" style="marginBottom: 0;" v-if="PowerCost.c">
        <div class="run-item" v-for="(item, index) in PowerCost.c" :key="index">
          <template v-if="item.value < 10000">
            <div class="p-title">{{ item.n }}({{ item.unit || '元' }})</div>
            <div class="p-desc">{{ Number(item.value).toFixed(1) }}</div>
          </template>
          <template v-else>
            <div class="p-title">{{ item.n }}(万{{ item.unit || '元' }})</div>
            <div class="p-desc">{{ (Number(item.value)/10000).toFixed(1) }}</div>
          </template>
        </div>
      </div>
    </template>
    
    <template v-if="CO2?.c?.length">
      <div class="title" style="marginTop: 0">
        <div class="t_name">{{ CO2.n }}</div>
      </div>
      <div class="process">
        <div class="processItem" v-for="(item, index) in CO2.c" :key="index">
          <template v-if="item.value < 10000">
            <div class="pro-title">
              <template v-if="item.value < 10000">
                <div class="pro-n">{{ item.n }} {{ item.unit }}</div>
                <div class="pro-num">{{ Number(item.value).toFixed(1) }}</div>
              </template>
            </div>
            <div class="pro-line">
              <div class="pro-m"></div>
              <div class="v-all flex " v-if="item.s">
                <div class="v" v-for="_item in changeValue(Number(item.value))" :key="_item"></div>
              </div>
            </div>
          </template>
          <template v-else>
            <div class="pro-title">
              <div class="pro-n">{{ item.n }} 万{{ item.unit }}</div>
              <div class="pro-num">{{ (Number(item.value)/10000).toFixed(0) }}</div>
            </div>
            <div class="pro-line">
              <div class="pro-m"></div>
              <div class="v-all flex " v-if="item.s">
                <div class="v" v-for="_item in changeValue((Number(item.value)/10000))" :key="_item"></div>
              </div>
            </div>
          </template>
        </div>
      </div>
    </template>
  </div>
  
  <template v-if="MidC?.c.length > 0">
    <div class="mid-top">
      <div class="m-item" v-for="(item, index) in MidC.c" :key="index">
        <template v-if="item.value < 10000">
          <div class="top-title">
            {{ item.n }}
            {{ item.unit }}
          </div>
          <div class="value flex flex-center">
            <div>
              <span v-for="(_item, _index) in Number(item.value)?.toFixed(1).split('.')[0]">{{ _item }}</span>
              <template v-if="+(Number(item.value)).toFixed(1).split('.')[1] > 0">
                <small>{{ '.' }}</small>
                <span v-for="(_item, _index) in Number(item.value)?.toFixed(1).split('.')[1]">{{ _item }}</span>
              </template>
            </div>
          </div>
        </template>
        <template v-else>
          <div class="top-title">
            {{ item.n }}
            万{{ item.unit }}
          </div>
          <div class="value flex flex-center">
            <div>
              <span v-for="(_item, _index) in (Number(item.value)/10000)?.toFixed(1).split('.')[0]">{{ _item }}</span>
              <template v-if="+(Number(item.value)).toFixed(1).split('.')[1] > 0">
                <small>{{ '.' }}</small>
                <span v-for="(_item, _index) in (Number(item.value)/10000)?.toFixed(1).split('.')[1]">{{ _item }}</span>
              </template>
            </div>
          </div>
        </template>
      </div>
    </div>
  </template>
  
  <div class="right">
    <template v-if="TemLine?.c?.length">
      <div class="title">
        <div class="t_name">{{ TemLine.n }}</div>
        <el-select class="select" v-model="TemLineC_dex" v-if="TemLine.c?.length > 1">
          <el-option
            v-for="(item, index) in TemLine.c"
            :key="index"
            :value="index"
            :label="item.n"
          />
        </el-select>
      </div>
      <Charts1
        :params="TemLine.c[TemLineC_dex].c"
        :currentDex="TemLineC_dex"
        :updateTimes="updateTimes"
        v-if="TemLine.c[TemLineC_dex].c?.length"
      />
    </template>
    
    <template v-if="PowerUse?.c?.length">
      <div class="title">
        <div class="t_name">{{ PowerUse.n }}</div>
      </div>
      <div class="powerUse tabLine">
        <el-icon @click="PowerUseGoFront()"><ArrowLeft /></el-icon>
        <div class="tabList">
          <div
            class="tabItem"
            :class="{'isActive': PowerUseC_dex === index}"
            v-for="(item, index) in PowerUse.c"
            @click="PowerUseClick(index)"
          >
            {{ item.n }}
          </div>
        </div>
        <el-icon @click="PowerUseGoAfter()"><ArrowRight /></el-icon>
      </div>
      <Charts1
        :params="PowerUse.c[PowerUseC_dex].c"
        :currentDex="PowerUseC_dex"
        v-if="PowerUse.c[PowerUseC_dex].c?.length"
      />
    </template>
    
    <div class="title">
      <div class="t_name">报警信息</div>
    </div>
    <slot></slot>
  </div>
</template>

<style lang="scss" scoped>
@font-face {
  font-family: "YouSheBiaoTiHei";
  src: url("/font/优设标题黑.ttf");
}
.left {
  left: 14px;
}
.right {
  right: 14px;
}
.left,
.right {
  position: absolute;
  top: 30px;
  width: 360px;
  padding: 0 10px 15px;
  background: url('../pic/02.png') no-repeat center;
  background-size: 100% 100%;
  .title {
    width: 336px;
    height: 46px;
    background: url('../pic/09.png') no-repeat center;
    background-size: 100% 100%;
    margin: 17px 0 10px;
    position: relative;
    .t_name {
      position: absolute;
      left: 19px;
      bottom: 6px;
      font-size: 18px;
      font-family: YouSheBiaoTiHei;
      color: #FFFFFF;
      padding-left: 20px;
      &::before {
        content: '';
        background: url('../pic/08.png') no-repeat center;
        background-size: 100% 100%;
        width: 13px;
        height: 17px;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
      }
    }
    .select {
      margin-left: auto;
      width: 100px;
    }
    :deep(.el-select__wrapper) {
      background-color: transparent;
      box-shadow: none;
      border: 1px solid rgba(8, 157, 132, 1);
    }
    :deep(.el-select__placeholder) {
      color: #fff;
    }
  }
  .run-list {
    margin: 15px 10px;
    display: flex;
    .run-item {
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      .run-num {
        font-size: 24px;
        font-family: YouSheBiaoTiHei;
        color: #FFFFFF;
        width: 90px;
        height: 78px;
        line-height: 78px;
        text-align: center;
        background: url('../pic/04.png') no-repeat center;
        background-size: 100% 100%;
      }
      .run-desc {
        margin-top: 10px;
        font-size: 14px;
        color: #FFFFFF;
        width: 100%;
        text-align: center;
      }
      .run-unit {
        font-size: 14px;
        color: #FFFFFF;
        width: 90px;
        height: 25px;
        text-align: center;
        background: url('../pic/07.png') no-repeat left center;
        background-size: 100% 100%;
      }
      .p-title {
        width: fit-content;
        text-align: center;
        height: 30px;
        line-height: 30px;
        background: linear-gradient(270deg, rgba(0, 0, 0, 0) 0%, rgba(255, 255, 255, 0.1) 41%, rgba(255, 255, 255, 0.1) 62%, rgba(0, 0, 0, 0) 100%);
        font-size: 14px;
        font-family: PingFangSC, PingFang SC;
        color: #FFFFFF;
      }
      .p-desc {
        margin-top: 20px;
        font-size: 24px;
        font-family: YouSheBiaoTiHei;
        color: #FFFFFF;
        width: 130px;
        height: 105px;
        text-align: center;
        background: url('../pic/05.png') no-repeat center;
        background-size: cover;
      }
    }
  }
  .process {
    padding: 10px 14px;
    .processItem {
      margin-bottom: 25px;
    }
    .pro-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .pro-n {
        font-size: 12px;
        font-family: PingFangSC, PingFang SC;
        color: #FFFFFF;
      }
      .pro-num {
        font-family: YouSheBiaoTiHei;
        font-size: 21px;
        color: #fff;
      }
    }
    .pro-line {
      position: relative;
      width: 100%;
      height: 14px;
      background: url('../pic/06.png') no-repeat center;
      background-size: 100% 100%;
      overflow: hidden;
      display: flex;
      .pro-m {
        position: absolute;
        left: 0;
        top: 0;
        margin: 2px;
        width: calc(100% - 4px);
        height: calc(100% - 4px);
        opacity: 1;
        z-index: 1;
        background: linear-gradient(90deg, rgba(0, 23, 26, 0.80) 0%, rgba(145, 238, 25, 0.05) 100%);
      }
      .v-all {
        margin: 2px;
        width: 100%;
        padding: 0 3px;
        .v {
          display: block;
          width: calc((100% - 60px) / 50);
          margin-right: 2px;
          background-color: rgba(145, 238, 25, 1);
          border-radius: 1px;
        }
      }
    }
  }
  .powerUse.tabLine {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 12px;
    width: 100%;
    margin-bottom: 18px;
    .tabList {
      width: fit-content;
      max-width: calc(100% - 32px);
      overflow: hidden;
      margin: 0 4px;
      height: 24px;
      border: 1px solid rgba(16,255,249,0.5);
      background: linear-gradient(180deg, rgba(3,95,102,0.6) 0%, rgba(3,95,102,0) 100%);
      display: flex;
      align-items: center;
      flex-wrap: nowrap;
      border-radius: 3px;
      .tabItem {
        width: fit-content;
        min-width: fit-content;
        height: 22px;
        line-height: 22px;
        padding: 0 12px;
        font-size: 12px;
        font-family: PingFangSC;
        font-weight: 500;
        color: rgba(16,255,249,0.7);
        cursor: pointer;
        border: 1px solid rgba(0,0,0,0);
        position: relative;
        &::after {
          content: ' ';
          position: absolute;
          // color: ;
          width: 0;
          height: 14px;
          right: -2px;
          top: 50%;
          transform: translateY(-50%);
          border: 1px solid;
          border-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.6), rgba(255, 255, 255, 0)) 1 1;
        }
      }
      .tabItem:nth-last-child(1) {
        &::after {
          border: none;
        }
      }
      .isActive {
        background: linear-gradient(180deg, rgba(16,255,249,0.6) 0%, rgba(3,95,102,0) 100%);
        border-radius: 3px;
        border: 1px solid #10FFF9;
        color: #D3F2EE;
        &::after {
          border: none;
        }
      }
    }
    :deep(.el-icon) {
      border-radius: 50%;
      border: none;
      width: 16px;
      height: 16px;
      background: linear-gradient(180deg, #0B888C 0%, #035F66 100%);
      box-shadow: inset 0px 2px 4px 0px rgba(11,136,140,0.44);
      padding: 2px;
      cursor: pointer;
      z-index: 0;
      &:hover {
        background: linear-gradient(180deg, #D3F2EE 0%, #5CCCC5 100%);
      }
      svg {
        width: 12px;
        height: 12px;
        color: #000 !important;
      }
    }
  }
}
.mid-top {
  position: fixed;
  width: 48.2vw;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  justify-content: space-between;
  .m-item {
    width: 100%;
    .top-title {
      font-size: 16px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 600;
      color: #FFFFFF;
      line-height: 20px;
      margin-bottom: 18px;
      width: 100%;
      text-align: center;
    }
    .value {
      flex: 1;
      div {
        width: fit-content;
        padding: 6px 10px 8px;
        background: linear-gradient(180deg, rgba(215,255,254,0.14) 0%, rgba(215,255,254,0.05) 7%, rgba(215,255,254,0) 50%, rgba(215,255,254,0.05) 93%, rgba(215,255,254,0.14) 100%);
        box-shadow: inset 0px -1px 0px 1px rgba(0,0,0,0.45), inset 0px 1px 0px 1px rgba(255,255,255,0.06);
        border-radius: 4px;
        text-align: center;
      }
      small {
        color: #fff;
        margin: 0 1px;
        font-size: 25px;
      }
      span {
        width: 30px;
        height: 40px;
        display: inline-block;
        background: url('../pic/card.png');
        background-size: 100% 100%;
        border-radius: 4px;
        text-align: center;
        font-size: 25px;
        color: #fff;
        font-weight: 800;
        line-height: 40px;
        margin: 0 1px;
      }
    }
  }
}
</style>